package com.fms.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fms.common.utils.JwtUtils;
import com.fms.common.vo.Result;
import com.fms.sys.entity.BlogArticle;
import com.fms.sys.service.IBlogArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 博客文章控制器
 * </p>
 *
 * @author Jinfeng
 * @since 2024-05-05
 */
@RestController
@RequestMapping("/blog/article")
public class BlogArticleController {

    @Autowired
    private IBlogArticleService blogArticleService;

    /**
     * 分页查询文章列表
     */
    @GetMapping("/list")
    public Result<Map<String, Object>> getArticleList(
            @RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "status", required = false) Integer status) {

        Page<BlogArticle> page = new Page<>(pageNo, pageSize);
        blogArticleService.getArticleList(page, title, categoryId, status);

        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }

    /**
     * 新增文章
     */
    @PostMapping
    public Result<?> addArticle(@RequestBody BlogArticle article) {
        if (article.getTitle() == null || article.getTitle().trim().isEmpty()) {
            return Result.fail("文章标题不能为空");
        }
        if (article.getContent() == null || article.getContent().trim().isEmpty()) {
            return Result.fail("文章内容不能为空");
        }
        article.setCreateBy(JwtUtils.getCurrentUserId());
        blogArticleService.addArticle(article);
        return Result.success("新增文章成功");
    }

    /**
     * 修改文章
     */
    @PutMapping
    public Result<?> updateArticle(@RequestBody BlogArticle article) {
        BlogArticle existingArticle = blogArticleService.getArticleById(article.getId());
        if (existingArticle == null || !existingArticle.getCreateBy().equals(JwtUtils.getCurrentUserId())) {
            return Result.fail("无权修改此文章");
        }
        blogArticleService.updateArticle(article);
        return Result.success("修改文章成功");
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteArticle(@PathVariable("id") Long id) {
        BlogArticle article = blogArticleService.getArticleById(id);
        if (article == null || !article.getCreateBy().equals(JwtUtils.getCurrentUserId())) {
            return Result.fail("文章不存在或无权删除");
        }
        blogArticleService.deleteArticle(id);
        return Result.success("删除文章成功");
    }

    /**
     * 获取文章详情
     */
    @GetMapping("/{id}")
    public Result<BlogArticle> getArticleById(@PathVariable("id") Long id) {
        BlogArticle article = blogArticleService.getArticleById(id);
        if (article == null) {
            return Result.fail("文章不存在");
        }
        // 增加浏览量
        blogArticleService.increaseViewCount(id);
        return Result.success(article);
    }

    /**
     * 更新文章状态
     */
    @PutMapping("/{id}/status/{status}")
    public Result<?> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("status") Integer status) {
        BlogArticle article = blogArticleService.getArticleById(id);
        if (article == null || !article.getCreateBy().equals(JwtUtils.getCurrentUserId())) {
            return Result.fail("文章不存在或无权修改");
        }
        blogArticleService.updateStatus(id, status);
        return Result.success(status == 1 ? "发布成功" : "保存为草稿成功");
    }

    /**
     * 点赞文章
     */
    @PutMapping("/{id}/like")
    public Result<?> likeArticle(@PathVariable("id") Long id) {
        blogArticleService.increaseLikeCount(id);
        return Result.success("点赞成功");
    }
}
